package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.dto.SysPositionGroupPermissionDto;
import cn.com.cifi.mars.bean.vo.sys.req.SaveSysPositionGroupPermissionReqParam;
import cn.com.cifi.mars.bean.vo.sys.rsp.PositionGroupPermissionVo;
import cn.com.cifi.mars.constant.IsDisableEnum;
import cn.com.cifi.mars.entity.common.SysPositionGroupPermission;
import cn.com.cifi.mars.entity.common.SysRolePermission;
import cn.com.cifi.mars.mapper.mars.SysPositionGroupPermissionMapper;
import cn.com.cifi.mars.service.SysPositionGroupPermissionService;
import cn.com.cifi.mars.util.UUIDUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 岗位组功能权限关联表 服务实现类
 * </p>
 *
 * @author yyd
 * @since 2019-09-20
 */
@Service
public class SysPositionGroupPermissionServiceImpl extends ServiceImpl<SysPositionGroupPermissionMapper, SysPositionGroupPermission> implements SysPositionGroupPermissionService {

	@Autowired
	private SysPositionGroupPermissionMapper sysPositionGroupPermissionMapper;

	/**
	 * 添加岗位功能权限
	 */
	@Transactional
	@Override
	public int add(SaveSysPositionGroupPermissionReqParam param, String name) {
		// 先删除岗位组关联数据
		sysPositionGroupPermissionMapper.deletePositionGroupId(param.getPositionGroupId());
		// 添加关联关系
		int num = 0;
		for (String permissionId : param.getPerIds()) {
			SysPositionGroupPermission bean = new SysPositionGroupPermission();
			bean.setPermissionId(permissionId);
			bean.setPositionGroupId(param.getPositionGroupId());
			bean.setCreateby(name);
			bean.setCreateTime(LocalDateTime.now());
			bean.setId(UUIDUtils.create());
			num = sysPositionGroupPermissionMapper.insert(bean);
		}
		return num;
	}

	/**
	 * 显示岗位功能权限
	 */
	@Override
	public PositionGroupPermissionVo querySysPositionGroupPermissionTree(String positionGroupId) {
		PositionGroupPermissionVo vo=new PositionGroupPermissionVo();
		List<SysPositionGroupPermissionDto> list=sysPositionGroupPermissionMapper.selectSysPositionGroupPermissionTree(IsDisableEnum.NO.getKey()+"",positionGroupId);
		//List<SysPositionGroupPermissionDto> listTree=buildTree(list,"");
		//JSONArray trssJson= JSONArray.parseArray(JSON.toJSONString(listTree));
		vo.setList(list);
		return vo;
	}
	
	/**
	 * 递归子节点
	 * @param list
	 * @param parentId
	 * @return
	 */
	public static List<SysPositionGroupPermissionDto> buildTree(List<SysPositionGroupPermissionDto> list,String parentId){
		List<SysPositionGroupPermissionDto> menus=new ArrayList<>();
	    for (SysPositionGroupPermissionDto menu : list) {  
	    	  //本身id
	          String menuId = menu.getPerCode();
	          //父id
	          String pid = menu.getParentCode();
	          if (parentId.equals(pid)) {  
	              List<SysPositionGroupPermissionDto> menuLists = buildTree(list, menuId);  
	              menu.setChildMenu(menuLists);
	              menus.add(menu);  
	          }  
	    }  
		return menus;
	}
}
